package cn.iocoder.yudao.module.im.dal.mysql.member;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.im.controller.app.member.vo.AppMemberConversationPageReqVO;
import cn.iocoder.yudao.module.im.dal.dataobject.member.MemberConversationDO;
import cn.iocoder.yudao.module.im.enums.ImGroupTypeEnum;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.im.controller.admin.member.vo.*;

import java.util.List;

/**
 * IM会话 Mapper
 *
 * @author Jerry
 */
@Mapper
public interface MemberConversationMapper extends BaseMapperX<MemberConversationDO> {

    default PageResult<MemberConversationDO> selectPage(MemberConversationPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MemberConversationDO>()
                .eqIfPresent(MemberConversationDO::getGroupId, reqVO.getGroupId())
                .eqIfPresent(MemberConversationDO::getGroupType, reqVO.getGroupType())
                .eqIfPresent(MemberConversationDO::getGroupLogo, reqVO.getGroupLogo())
                .eqIfPresent(MemberConversationDO::getByUserId, reqVO.getByUserId())
                .eqIfPresent(MemberConversationDO::getUserId, reqVO.getUserId())
                .betweenIfPresent(MemberConversationDO::getLastMessageTime, reqVO.getLastMessageTime())
                .eqIfPresent(MemberConversationDO::getLastMessageContent, reqVO.getLastMessageContent())
                .eqIfPresent(MemberConversationDO::getLastMessageContentType, reqVO.getLastMessageContentType())
                .eqIfPresent(MemberConversationDO::getLastMessageReadStatus, reqVO.getLastMessageReadStatus())
                .eqIfPresent(MemberConversationDO::getPinned, reqVO.getPinned())
                .eqIfPresent(MemberConversationDO::getUnreadMessageCount, reqVO.getUnreadMessageCount())
                .betweenIfPresent(MemberConversationDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MemberConversationDO::getId));
    }

    default PageResult<MemberConversationDO> selectPage(AppMemberConversationPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MemberConversationDO>()
                .eqIfPresent(MemberConversationDO::getGroupId, reqVO.getGroupId())
                .eqIfPresent(MemberConversationDO::getGroupType, reqVO.getGroupType())
                .eqIfPresent(MemberConversationDO::getGroupLogo, reqVO.getGroupLogo())
                .eqIfPresent(MemberConversationDO::getByUserId, reqVO.getByUserId())
                .eqIfPresent(MemberConversationDO::getUserId, reqVO.getUserId())
                .betweenIfPresent(MemberConversationDO::getLastMessageTime, reqVO.getLastMessageTime())
                .eqIfPresent(MemberConversationDO::getLastMessageContent, reqVO.getLastMessageContent())
                .eqIfPresent(MemberConversationDO::getLastMessageContentType, reqVO.getLastMessageContentType())
                .eqIfPresent(MemberConversationDO::getLastMessageReadStatus, reqVO.getLastMessageReadStatus())
                .eqIfPresent(MemberConversationDO::getPinned, reqVO.getPinned())
                .eqIfPresent(MemberConversationDO::getStatus, CommonStatusEnum.ENABLE.getStatus())
                .eqIfPresent(MemberConversationDO::getUnreadMessageCount, reqVO.getUnreadMessageCount())
                .betweenIfPresent(MemberConversationDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MemberConversationDO::getPinned, MemberConversationDO::getLastMessageTime, MemberConversationDO::getUpdateTime));
    }

    default MemberConversationDO getMemberConversationByByUserIdAndUserId(Long byUserId, Long userId) {
        return selectOne(new LambdaQueryWrapperX<MemberConversationDO>()
                .eq(MemberConversationDO::getGroupType, ImGroupTypeEnum.PRIVATE.getType())
                .eq(MemberConversationDO::getByUserId, byUserId)
                .eq(MemberConversationDO::getUserId, userId));
    };

    default MemberConversationDO getMemberConversationByByUserIdAndGroupId(Long byUserId, Long groupId) {
        return selectOne(new LambdaQueryWrapperX<MemberConversationDO>()
                .eq(MemberConversationDO::getGroupType, ImGroupTypeEnum.PRIVATE.getType())
                .eq(MemberConversationDO::getByUserId, byUserId)
                .eq(MemberConversationDO::getGroupId, groupId));
    };

    default List<MemberConversationDO> getMemberConversationByGroupId(Long groupId){
        return selectList(new LambdaQueryWrapperX<MemberConversationDO>()
                .eq(MemberConversationDO::getGroupId, groupId));
    }

    default void updateUnReadCount(Long groupId, Long receiveUserId, Long unReadCount){
        LambdaUpdateWrapper<MemberConversationDO> updateWrapper = new LambdaUpdateWrapper<MemberConversationDO>()
                .set(MemberConversationDO::getUnreadMessageCount, unReadCount)
                .eq(MemberConversationDO::getGroupId, groupId)
                .eq(MemberConversationDO::getByUserId, receiveUserId);
        update(null, updateWrapper);
    }

    default void deleteMemberConversation(Long id){
        LambdaUpdateWrapper<MemberConversationDO> updateWrapper = new LambdaUpdateWrapper<MemberConversationDO>()
                .set(MemberConversationDO::getStatus, CommonStatusEnum.DISABLE.getStatus())
                .eq(MemberConversationDO::getId, id);
        update(null, updateWrapper);
    }
}